const http = require('http');
const hostname = '127.0.0.1';
const port = 12345;
const server = http.createServer((req, res) => {                //创建一个HTTP服务
    res.statusCode = 200;
    // res.writeHead(200,{'Content-Type':'text/plain;charset=utf-8'});
    res.setHeader('Content-Type', 'text/plain;charset=utf-8');
    res.end('服务启动成功！\n');
});
server.listen(port, hostname, () => {                           //监听指定端口并启动服务
    console.log(`Server running at http://${hostname}:${port}/`);
});



module.exports = function (param, cb) {
    asyncFun1(param, function (er, data) {
      if (er) return cb(er);

      asyncFun2(data,function (er,data) {
        if (er) return cb(er);

        asyncFun3(data, function (er, data) {
          if (er) return cb(er);
          cb(data);
        })
      })
    })
  }


var co = require('co');
 
co(function *(){
  // yield any promise 
  var result = yield Promise.resolve(true);
}).catch(onerror);
 
co(function *(){
  // resolve multiple promises in parallel 
  var a = Promise.resolve(1);
  var b = Promise.resolve(2);
  var c = Promise.resolve(3);
  var res = yield [a, b, c];
  console.log(res);
  // => [1, 2, 3] 
}).catch(onerror);
 
// errors can be try/catched 
co(function *(){
  try {
    yield Promise.reject(new Error('boom'));
  } catch (err) {
    console.error(err.message); // "boom" 
 }
}).catch(onerror);
 
function onerror(err) {
  // log any uncaught errors 
  // co will not throw any errors you do not handle!!! 
  // HANDLE ALL YOUR ERRORS!!! 
  console.error(err.stack);
}

var PageQuery = async (page, pageSize, Model, populate, queryParams, sortParams) => {
    var start = (page - 1) * pageSize;
    var $page = {
        pageNumber: page
    };

    let TotleRow = await ModelCount(Model, queryParams);
    let records = await PageRecords(Model, queryParams, start, pageSize, populate, sortParams);

    $page.TotleRow = TotleRow;//(count - 1) / pageSize + 1;
    $page.PageCount=parseInt(TotleRow/pageSize)+1;
    $page.results = records;
    return $page;
};

var ModelCount = (Model, queryParams) => {
    return Model.count(queryParams).exec().then((count) => {
        return count;
    }).catch((err) => {
        console.log("err:" + err);
        return err;
    });
}

var PageRecords = (Model, queryParams, start, pageSize, populate, sortParams) => {
    return Model
        .find(queryParams)
        .skip(start)
        .limit(pageSize)
        .populate(populate)
        .sort(sortParams)
        .exec()
        .then((doc) => {
            return doc;
        }).catch((err) => {
            console.log("err:" + err);
            return err;
        });
}

//......
Customer.PageQuery(pageNum, pageSize, Customer.Model, "", {}, {}).then((pageResult) => {

        res.render('customersList', {
            layout: "admin",
            customersList: pageResult.results,
            totalPages: pageResult.PageCount,
            pno: pageNum,
        });
    }).catch((err) => {
        console.log("err:" + err);
        res.send("err");
    });